package com.hh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.entity.Category;
import com.hh.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 辉辉 2022/8/16 20:37
 */
@Api(tags = "商品分类")
@RestController
@RequestMapping("/prod/category/")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/table")
    @ApiOperation("商品分类的分页查询")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadCategoryList() {
        List<Category> data = categoryService.findList();
        return ResponseEntity.ok(data);
    }

    @GetMapping("/listCategory")
    @ApiOperation("商品分类的所有父节点")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> listCategory() {
        List<Category> data = categoryService.findRootCategory();
        return ResponseEntity.ok(data);
    }

    @PostMapping
    @ApiOperation("新增商品分类")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public ResponseEntity<Void> addCategory(@RequestBody Category category) {
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }

    // ------------前台------------

    @GetMapping("/category/categoryInfo")
    @ApiOperation("前台根据父节点查询分类")
    public ResponseEntity<List<Category>> categoryInfo( Long parentId) {
        List<Category> data = categoryService.findCategoryByParentId(parentId);
        return ResponseEntity.ok(data);
    }
}
